﻿using MediatR;
using W.Warehouse.Domain.DTO;
using W.Warehouse.Read.Api.Application.Command.CheckCommand;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Read.Api.Application.CommandHandlder.CheckCommandHandler
{
    public class StatisticsModelCommandHandler : IRequestHandler<StatisticsModelCommand, StatisticModelDTo>
    {
        private readonly ICheckRepository _checkRepository;
        private readonly IStatisticsRepository _statisticsRepository;
        private readonly IStorageProductsRepository _storageProductsRepository;
        private readonly IOutStorageProductsRepository _outStorageProductsRepository;
        public StatisticsModelCommandHandler(ICheckRepository checkRepository, IStatisticsRepository statisticsRepository, IStorageProductsRepository storageProductsRepository, IOutStorageProductsRepository outStorageProductsRepository)
        {
            _checkRepository = checkRepository;
            _statisticsRepository = statisticsRepository;
            _storageProductsRepository = storageProductsRepository;
            _outStorageProductsRepository = outStorageProductsRepository;
        }

        public async Task<StatisticModelDTo> Handle(StatisticsModelCommand request, CancellationToken cancellationToken)
        {
            var statistics = await _statisticsRepository.GetAllAsync();
            var storages = await _storageProductsRepository.GetAllAsync();
            var outstorages = await _outStorageProductsRepository.GetAllAsync();
           
            var check = from a in await _checkRepository.GetAllAsync()
                        join b in statistics on a.CheckId equals b.CheckId
                        join c in storages on b.ProductNo equals c.ProductNo
                        join d in outstorages on b.ProductNo equals d.ProductNo
                        where a.IsDelete == false && a.CheckId == request.CheckId && b.IsDelete == false && c.IsDelete == false 
                        select new StatisticModelDTo
                        {
                            CheckId = a.CheckId,
                            CheckNo = a.CheckNo,
                            AssociateNumber = a.AssociateNumber,
                            CheckMakeHe = a.CheckMakeHe,
                            CheckDate = a.CheckDate,
                            CheckNotes = a.CheckNotes,
                            ProductId = a.ProductId,
                            AuditStatus = a.AuditStatus,
                            AuditStatusText = a.AuditStatus.ToString(),
                            CheckType = a.CheckType,
                            office_id = a.office_id,
                            user_id = a.user_id,
                            ExamineReason = a.ExamineReason,
                            ExaminePeople = a.ExaminePeople,
                            ExamineTime = a.ExamineTime,
                            StatisticsList = statistics.Where(x => x.CheckId == a.CheckId).Select(x => new Statistics
                            {
                                StorageNum = c.StorageNum,
                                StatisticsId = x.StatisticsId,
                                ProductName = x.ProductName,
                                ProductNo = x.ProductNo,
                                ProductSpec = x.ProductSpec,
                                StatisticsBatch = x.StatisticsBatch,
                                UnitId = x.UnitId,
                                BinLocationName = x.BinLocationName,
                                office_id = x.office_id,
                                user_id = x.user_id,
                                StatisticsSum = x.StatisticsSum,
                                CheckId = x.CheckId,
                                KuNum=c.StorageNum-d.OutStorageNum,
                            }).ToList()


                        };
            return check.FirstOrDefault();
        }
    }
}
